最大子序和给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例:123输入: [-2,1,-3,4,-1,2,1,-5,4]输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 进阶: 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-subarray Code:96%95%1234567891011121314151617181920212223242526class Solution { public int maxSubArray(int[] nums) { if(nums==null||nums.length==0)return 0; int max=Integer.MIN_VALUE; int pre=Integer.MIN_VALUE; //局部最优解 int nice=Integer.MIN_VALUE; int[] list=new int[nums.length]; for(int i=0;i<nums.length;i++) { //局部优解不优于前一个 int temp=(pre>nice)?pre:nice; pre=nums[i]; if(temp<0) { nice=nums[i]; }else{ nice=nums[i]+temp; } if(nice>max)max=nice; } //if(pre>max)return pre; return max; }}文章作者: Paakciu文章链接: http://paakciu.github.io/2020/11/11/leetcode%20%E6%9C%80%E5%A4%A7%E5%AD%90%E5%BA%8F%E5%92%8C/版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Paakciu!javaleetcode字节跳动简单数组动态规划分治算法 打赏wechatalipay上一篇leetcode 三角形最小路径和下一篇leetcode 最大正方形 相关推荐 2020-11-11leetcode 买卖股票的最佳时机 II 2020-11-08leetcode 买卖股票的最佳时机 2020-11-11leetcode 三角形最小路径和 2020-11-11leetcode 合并两个排序的链表 2020-11-11leetcode 按键持续时间最长的键 2020-11-11leetcode 最长连续递增序列